package com.foreknow.dao.impl;

import com.foreknow.bean.User;
import com.foreknow.dao.UserDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public boolean save(User user)throws SQLException {
        String sql = "insert into users(adminName,password) values(?,?)";
        int isRigth = jdbcTemplate.update(sql,user.getAdminName(),user.getPassword());
        return isRigth == 1;
    }

    @Override
    public User login(String adminName, String password)throws SQLException {
        String sql = "select * from users where adminname = ? and password = ?";
        EntityMapping mapping = mappingFactory.getmapping(MappingFactory.USER_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping,adminName,password);
        if(list.size()>0) {
            return (User) list.get(0);
        }
        return null;
    }

//    public static void main(String[] args) {
//        UserDao userDao = new UserDaoImpl();
//        User user = new User();
//        user.setAdminName("xxx");
//        user.setPassword("123456");
//        boolean isRight = userDao.save(user);
//        if(isRight) {
//            System.out.println("success......");
//        }else {
//            System.out.println("error......");
//        }
//    }
}
